package com.xu.demo.common.config;


import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * http://localhost:8082/swagger-ui/index.html
 */
@Configuration
public class OpenApiConfig {

    @Bean
    public OpenAPI openApi() {
        return new OpenAPI()
                .info(buildInfo()
                .version("v2.0"))
                .externalDocs(new ExternalDocumentation()
                        .description("The Open API"));
                //再在这里添加上Swagger要使用的安全策略
                //.addSecurityItem(new SecurityRequirement().addList("Authorization"))
                //.components(components());
    }

    public Info buildInfo() {
        return new Info()
                .title("文档标题")
                .description("文档描述")
                .contact(buildContact())
                .license(new License().name("Apache"));
    }

    public Contact buildContact() {
        return new Contact()
                .name("作者").email("邮箱").url("可以写你的博客地址或不填");
    }

    // 1. 先在组件中注册安全策略
    private Components components() {
        return new Components()
                // 第一个参数是key值，后面是初始化一个安全策略的参数
                .addSecuritySchemes("Authorization",
                        new SecurityScheme()
                                .name("Authorization")
                                .type(SecurityScheme.Type.HTTP)
                                .scheme("bearer")
                                .in(SecurityScheme.In.HEADER).name("Authorization"));
    }
}


